package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Lenovo on 2017/4/4.
 */
public class BusinessAdAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<AdBannerInfo> datas;
    private List<AdBannerInfo> bussList = new ArrayList<>();

    public BusinessAdAdapter(Context context, List<AdBannerInfo> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = getViewHolderByViewType(viewType);
        return holder;
    }


    private RecyclerView.ViewHolder getViewHolderByViewType(int viewType) {
        View AdBannerView = View.inflate(context, R.layout.item_business_ad_banner, null);
        View AdNormalView = View.inflate(context, R.layout.item_business_nomal, null);

        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 1:
                holder = new BusinessAdBanner_ViewHolder(AdBannerView);
                break;
            case 2:
                holder = new BusinessAdNomal_ViewHolder(AdNormalView);
                break;
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        int type = -1;
        if (datas.get(position).getAdvert_type_id() == 1) {
            type = 1;
        } else if (datas.get(position).getAdvert_type_id() != 1) {
            type = 2;
        }
        return type;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final AdBannerInfo data = datas.get(position);
        int typeId = data.getAdvert_type_id();
        if (typeId == 1) {
            //   ImageLoader.getInstance().load(context, ((BusinessAdBanner_ViewHolder) holder).img, data.getAdvert_detail().getPic());
            GlideUtils.loadImage(context,data.getAdvert_detail().getPic(),((BusinessAdBanner_ViewHolder) holder).img);

//            ((BusinessAdBanner_ViewHolder) holder).checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    if (isChecked) {
//                        bussList.add(data);
//                    } else {
//                        bussList.remove(data);
//                    }
//                }
//            });
            data.getAdvert_detail().setSelected(data.getAdvert_detail().getSelected() != null ? data.getAdvert_detail().getSelected() : false);
            ((BusinessAdBanner_ViewHolder) holder).bannerAd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!v.isPressed()) {
                        return;
                    }
                    data.getAdvert_detail().setSelected(!data.getAdvert_detail().getSelected());
                    ((BusinessAdBanner_ViewHolder) holder).checkBox.setChecked(data.getAdvert_detail().getSelected());
                    if (data.getAdvert_detail().getSelected()) {
                        bussList.add(data);
                    } else {
                        bussList.remove(data);
                    }
                }
            });


        } else if (typeId != 1) {
            if (data.getAdvert_type_id() == 6) {
                ((BusinessAdNomal_ViewHolder) holder).info.setTextColor(context.getResources().getColor(R.color.new_red));
                ((BusinessAdNomal_ViewHolder) holder).info.setTextSize(12);
                ((BusinessAdNomal_ViewHolder) holder).info.setText(data.getAdvert_detail().getProduct_info().getPrice());
                ((BusinessAdNomal_ViewHolder) holder).name.setText(data.getAdvert_detail().getProduct_info().getName());
                //   ImageLoader.getInstance().load(context, ((BusinessAdNomal_ViewHolder) holder).img, data.getAdvert_detail().getProduct_info().getPic());
                GlideUtils.loadImage(context,data.getAdvert_detail().getProduct_info().getPic(),((BusinessAdNomal_ViewHolder) holder).img);

            } else {
                ((BusinessAdNomal_ViewHolder) holder).name.setText(data.getAdvert_detail().getName());
                ((BusinessAdNomal_ViewHolder) holder).info.setText(data.getAdvert_detail().getDes());
                //     ImageLoader.getInstance().load(context, ((BusinessAdNomal_ViewHolder) holder).img, data.getAdvert_detail().getPic());
                GlideUtils.loadImage(context,data.getAdvert_detail().getPic(),((BusinessAdNomal_ViewHolder) holder).img);

            }
//            ((BusinessAdNomal_ViewHolder) holder).checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    if (isChecked) {
//                        bussList.add(data);
//                    } else {
//                        bussList.remove(data);
//                    }
//                }
//            });
            data.getAdvert_detail().setSelected(data.getAdvert_detail().getSelected() != null ? data.getAdvert_detail().getSelected() : false);
            ((BusinessAdNomal_ViewHolder) holder).businessAd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!v.isPressed()) {
                        return;
                    }
                    data.getAdvert_detail().setSelected(!data.getAdvert_detail().getSelected());
                    ((BusinessAdNomal_ViewHolder) holder).checkBox.setChecked(data.getAdvert_detail().getSelected());
                    if (data.getAdvert_detail().getSelected()) {
                        bussList.add(data);
                    } else {
                        bussList.remove(data);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datas != null ? datas.size() : 0;
    }


    public List<AdBannerInfo> getBussList() {
        return bussList;
    }

    class BusinessAdBanner_ViewHolder extends RecyclerView.ViewHolder {
        public View view;
        ImageView img;
        AppCompatCheckBox checkBox;
        LinearLayout bannerAd;

        public BusinessAdBanner_ViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            this.img = (ImageView) itemView.findViewById(R.id.banner_img);
            this.checkBox = (AppCompatCheckBox) itemView.findViewById(R.id.busAdCheck);
            this.bannerAd = (LinearLayout) itemView.findViewById(R.id.rl_BannerAd);
        }
    }

    class BusinessAdNomal_ViewHolder extends RecyclerView.ViewHolder {
        public View view;
        ImageView img;
        TextView name, info;
        AppCompatCheckBox checkBox;
        RelativeLayout businessAd;

        public BusinessAdNomal_ViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            this.img = (ImageView) itemView.findViewById(R.id.headImg);
            this.name = (TextView) itemView.findViewById(R.id.tvName);
            this.info = (TextView) itemView.findViewById(R.id.tvInfo);
            this.checkBox = (AppCompatCheckBox) itemView.findViewById(R.id.busAdCheck);
            this .businessAd =(RelativeLayout)itemView.findViewById(R.id.businessAd);
        }
    }
}